#!/bin/bash -x

PREFIX="/tools/"
SOURCES="${BUILD}/decompress_src"
mkdir  -pv ${SOURCES}

##############################################
####   compile binutils pass1           ######
##############################################
function build_binutils_pass1(){
	cd ${SOURCES}
	tar jxvf ${SRC}/binutils-2.20.1.tar.bz2 

	## in a different diretory will make the make command failed, don't know why:(
	mv "binutils-2.20.1" "binutils-2.20.1_pass1"
	mkdir ${BUILD}/binutils_pass1
	cd ${BUILD}/binutils_pass1
	"${SOURCES}/binutils-2.20.1_pass1/configure" --target=${LFS_TGT} --prefix=${PREFIX} --disable-nls --disable-werror
	if [ $? -ne 0 ]; then
	    exit 1
	fi
	make
	if [ $? -ne 0 ]; then
	    exit 1
	fi
    case $(uname -m) in
        x86_64) mkdir -v ${PREFIX}/lib && ln -sv lib ${PREFIX}/lib64 ;;
    esac
	make install
	if [ $? -ne 0 ]; then
	    exit 1
	fi
}
##############################################
####   compile binutils pass2           ######
##############################################
function build_binutils_pass2(){
	cd ${SOURCES}
	tar jxvf ${SRC}/binutils-2.20.1.tar.bz2 

	## in a different diretory will make the make command failed, don't know why:(
	mv "binutils-2.20.1" "binutils-2.20.1_pass2"
	mkdir ${BUILD}/binutils_pass2
	cd ${BUILD}/binutils_pass2
        CC="${LFS_TGT}-gcc -B/tools/lib" AR=${LFS_TGT}-ar RANLIB=${LFS_TGT}-ranlib "${SOURCES}/binutils-2.20.1_pass2/configure" --prefix=${PREFIX} --disable-nls --with-lib-path=${PREFIX}/lib
	if [ $? -ne 0 ]; then
	    exit 1
	fi
	make
	if [ $? -ne 0 ]; then
	    exit 1
	fi
	make install
	if [ $? -ne 0 ]; then
	    exit 1
	fi
    make -C ld clean
    make -C ld LIB_PATH=/usr/lib:/lib
    cp -v ld/ld-new /tools/bin
}
##############################################
####   compile gcc pass1                ######
##############################################
function build_gcc_pass1(){
	cd ${SOURCES}
	tar jxvf ${SRC}/gcc-4.5.1.tar.bz2 
	mv gcc-4.5.1 gcc-4.5.1_pass1
	cd gcc-4.5.1_pass1

	tar jxvf ${SRC}/mpfr-3.0.0.tar.bz2 
	tar jxvf ${SRC}/gmp-5.0.1.tar.bz2 
	tar zxvf ${SRC}/mpc-0.8.2.tar.gz 
	mv gmp-5.0.1/ gmp
	mv mpfr-3.0.0/ mpfr
	mv mpc-0.8.2/ mpc

	mkdir ${BUILD}/gcc_pass1
	cd ${BUILD}/gcc_pass1
	${SOURCES}/gcc-4.5.1_pass1/configure --target=${LFS_TGT} --prefix=${PREFIX} --disable-nls --disable-shared --disable-multilib --disable-decimal-float --disable-threads --disable-libmudflap --disable-libssp --disable-libgomp --enable-languages=c --with-gmp-include=$(pwd)/gmp --with-gmp-lib=$(pwd)/gmp/.libs --without-ppl --without-cloog
	if [ $? -ne 0 ]; then
	    exit 1
	fi
	make
	if [ $? -ne 0 ]; then
	    exit 1
	fi
	make install
	if [ $? -ne 0 ]; then
	    exit 1
	fi
	path_libgcc=`$LFS_TGT-gcc -print-libgcc-file-name`
	path_libgcc_eh=`echo ${path_libgcc} |sed 's/libgcc/&_eh/'`
	ln -vs ${path_libgcc}  ${path_libgcc_eh}
}

##############################################
####   compile gcc pass2                ######
##############################################
function build_gcc_pass2(){
	cd ${SOURCES}
	tar jxvf ${SRC}/gcc-4.5.1.tar.bz2 
	mv gcc-4.5.1 gcc-4.5.1_pass2
	cd gcc-4.5.1_pass2
        patch -Np1 -i ${SRC}/gcc-4.5.1-startfiles_fix-1.patch

        cp -v gcc/Makefile.in{,.orig}
        sed 's@\./fixinc\.sh@-c true@' gcc/Makefile.in.orig >gcc/Makefile.in

        cp -v gcc/Makefile.in{,.tmp}
        sed 's/^T_CFLAGS =$/& -fomit-frame-pointer/' gcc/Makefile.in.tmp >gcc/Makefile.in
        
        for file in $(find gcc/config -name linux64.h -o -name linux.h -o -name sysv4.h)
        do 
                 cp -uv ${file}{,.orig}
                 sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&@g' -e 's@/usr@/tools@g' ${file}.orig >${file}
                 echo '
#undef STANDARD_INCLUDE_DIR
#define STANDARD_INCLUDE_DIR 0
#define STANDARD_STARTFILE_PREFIX_1 ""
#define STANDARD_STARTFILE_PREFIX_2 ""' >>${file}
                touch ${file}.orig
        done

        case $(uname -m) in
                x86_64)
                        for file in $(find gcc/config -name t-linux64); do 
                                cp -v ${file}{,.orig}
                                sed '/MULTILIB_OSDIRNAMES/d' ${file}.orig >${file}
                        done
                ;;
        esac

	tar jxvf ${SRC}/mpfr-3.0.0.tar.bz2 
	tar jxvf ${SRC}/gmp-5.0.1.tar.bz2 
	tar zxvf ${SRC}/mpc-0.8.2.tar.gz 
	mv gmp-5.0.1/ gmp
	mv mpfr-3.0.0/ mpfr
	mv mpc-0.8.2/ mpc

	mkdir ${BUILD}/gcc_pass2
	cd ${BUILD}/gcc_pass2
	CC="${LFS_TGT}-gcc -B/tools/lib" AR=${LFS_TGT}-ar RANLIB=${LFS_TGT}-ranlib  ${SOURCES}/gcc-4.5.1_pass2/configure --prefix=${PREFIX} --with-local-prefix=${PREFIX} --enable-clocale=gnu --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-languages=c,c++ --disable-libstdcxx-pch --disable-multilib --disable-bootstrap --disable-libgomp --with-gmp-include=$(pwd)/gmp --with-gmp-lib=$(pwd)/gmp/.libs --without-ppl --without-cloog
	if [ $? -ne 0 ]; then
	    exit 1
	fi
	make
	if [ $? -ne 0 ]; then
	    exit 1
	fi
	make install
	if [ $? -ne 0 ]; then
	    exit 1
	fi
        ln -vs `which gcc` /tools/bin/cc
}

##############################################
####   install API Headers              ######
##############################################
function install_API_header(){
	cd ${SOURCES}
	tar jxvf ${SRC}/linux-2.6.35.4.tar.bz2
	cd linux-2.6.35.4/
	make mrproper
	if [ $? -ne 0 ]; then
	    exit 1
	fi
	make headers_check
	if [ $? -ne 0 ]; then
	    exit 1
	fi
	make INSTALL_HDR_PATH=dest headers_install
	if [ $? -ne 0 ]; then
	    exit 1
	fi
	cp -rv dest/include/* ${PREFIX}/include/
}

##############################################
####   install Glibc                    ######
##############################################
function build_glibc(){
	cd ${SOURCES}
	tar jxvf ${SRC}/glibc-2.12.1.tar.bz2
	cd glibc-2.12.1/
	patch -Np1 -i ${SRC}/glibc-2.12.1-gcc_fix-1.patch 
	patch -Np1 -i ${SRC}/glibc-2.12.1-makefile_fix-1.patch 

	mkdir ${BUILD}/glibc-build
	cd ${BUILD}/glibc-build/

	case `uname -m` in
	    i?86) echo "CFLAGS += -march=i486 -mtune=native" > configparms ;;
	esac
	#${SOURCES}/glibc-2.12.1/configure --prefix=${PREFIX} --host=$LFS_TGT --build=$(${SOURCES}/glibc-2.12.1/scripts/config.guess) --disable-profile --enable-add-ons --enable-kernel=2.6.22.5 --with-headers=${PREFIX}/include libc_cv_forced_unwind=yes libc_cv_c_cleanup=yes
	${SOURCES}/glibc-2.12.1/configure --prefix=${PREFIX} --host=$LFS_TGT --build=$(${SOURCES}/glibc-2.12.1/scripts/config.guess) --disable-profile --enable-add-ons --with-headers=${PREFIX}/include libc_cv_forced_unwind=yes libc_cv_c_cleanup=yes
	if [ $? -ne 0 ]; then
	    exit 1
	fi
	make
	if [ $? -ne 0 ]; then
	    exit 1
	fi
	make install
	if [ $? -ne 0 ]; then
	    exit 1
	fi
}

##############################################
####   test toolchain                   ######
##############################################
function test_toolchain(){
        if [ "$1" == "" ]; then
                echo "no cc command specified"
                exit 1
        fi
        CMD_CC="$1"
	echo 'main(){}' > dummy.c
	${CMD_CC} -B/tools/lib dummy.c
        if [ $? -ne 0 ]; then
            echo 'compile dummy failed'
            exit 1
        fi
	readelf -l a.out |grep ': /tools'
        if [ $? -ne 0 ]; then
            echo 'no lib in /tools linked'
            exit 1
        fi
	rm -v dummy.c a.out
}

function install_toolchain(){

	build_binutils_pass1
	build_gcc_pass1
	install_API_header
	build_glibc

	##############################################
	####   adjust toolchain                 ######
	##############################################
	SPECS=`dirname $($LFS_TGT-gcc -print-libgcc-file-name)`/specs
	$LFS_TGT-gcc -dumpspecs|sed -e 's@/lib\(64\)\?/ld@/tools&@g' -e "/^\*cpp:$/{n;s,$, -isystem /tools/include,}" > ${SPECS}
	echo "New specs file is: ${SPECS}"
	unset SPECS

    test_toolchain "${LFS_TGT}-gcc"

	build_binutils_pass2
	build_gcc_pass2
    test_toolchain "cc"
}

install_toolchain
